home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 140_01.zip / MGAME.C < prev    next >
Text File  |  1993-06-26  |  3KB  |  115 lines

  1. /* New maths game for kids, based on an idea by Michael Mannington
  2.  
  3.      By    Bill Bolton,
  4.     Software Tools,
  5.     P.O. Box 80,
  6.     NSW, 2106,
  7.     AUSTRALIA
  8.  
  9.     June 14, 1981    Version 1.0 (Initial Release)
  10.  
  11. */    
  12.  
  13. #include BDSCIO.H    /* useful definitions etc. */
  14.  
  15. #define STDIN 0        /* CP/M console input */
  16. #define STDOUT 1    /* CP/M console output */
  17. #define SCALE 10    /* Maximum value of random number, change to make the
  18.                game harder as child gets older */
  19.  
  20. int    odevice;    /* All functions should know about I/O devices */
  21. int    idevice;
  22.  
  23. main()
  24.  
  25. {
  26.     int    right;    /* Number of correct answers */
  27.     int    plays;    /* Number of turns */ 
  28.  
  29.     right = 0;    /* Initialisation */
  30.     plays = 0;
  31.     odevice = STDOUT;    /* Change for different I/O */
  32.     idevice = STDIN;
  33.  
  34.     /* Set up the game */
  35.  
  36.     fprintf(odevice,"\n\tSoftware Tools Presents\n");
  37.     fprintf(odevice,"\n\tLETS PLAY......NUMBERS\n\n");
  38.     srand1("Press any key to start.\n\n");
  39.     getchar(); /* Clear answer to srand1 prompt from input */
  40.  
  41.     /* Play the game, always at least one turn */
  42.  
  43.     do {
  44.         turn(&right,&plays);
  45.     } while(more());
  46.  
  47.     /* Report results and say goodbye */
  48.  
  49.     fprintf(odevice,"Thanks for playing, you got %d right from %d turns\n",
  50.             right,plays);
  51. }
  52.  
  53. /* Gets two randomly generated numbers and asks the question, evaluates the
  54.    the answer and displays the response, checks for numeric answer */
  55.  
  56. turn(right,plays)
  57.  
  58. int    *right;        /* pointer to munber of right answers */
  59. int    *plays;        /* pointer to number of turns */
  60.  
  61. {
  62.     int    first;    /* first randomly generated  number */
  63.     int    second;    /* second  ditto */
  64.     int    result;    /* players answer as a number */
  65.     char    answer[10];    /* players answer as a string */
  66.  
  67.     do {
  68.         first = rand()%SCALE;    /* change SCALE in header to change */
  69.         second = rand()%SCALE;    /* range of random numbers */
  70.     } while((first + second) == 0); /* eliminate 0 + 0 */ 
  71.     ask(first,second,answer);
  72.     while((result = atoi(answer)) == 0) { /* only accept numbers */
  73.         fprintf(odevice,"You MUST answer with a number, try again.\n\n");
  74.         ask(first,second,answer);
  75.     }
  76.  
  77.     /* test answer for correctness, only two choices : right or wrong */
  78.  
  79.     if(result == (first + second)) {
  80.         fprintf(odevice,"GREAT, you got it RIGHT !\n\n");
  81.         (*right)++;
  82.     }
  83.     else {
  84.         fprintf(odevice,"Sorry, the right answer was %d\n\n",
  85.             first + second);
  86.     }
  87.     (*plays)++;
  88. }
  89.  
  90. /* Asks the question and gets the players answer as a string */
  91.  
  92. ask(first,second,answer)
  93.  
  94. int     first;    /* local copy of first number */
  95. int    second;    /* local copy of second number */
  96. char    *answer;    /* pointer to string */
  97.  
  98. {
  99.     fprintf(odevice,"What is   %d  +  %d     ",first,second);
  100.     fgets(answer,idevice);
  101. }
  102.  
  103. /* Asks if the player wants another turn, returns 1 if Yes */
  104.  
  105. more()
  106.  
  107. {
  108.     char    c;
  109.  
  110.     fprintf(odevice,"Another turn ? .... type N for NO or Y for YES....");
  111.     c = getc(idevice);
  112.     fprintf(odevice,"\n\n");
  113.     return((toupper(c) == 'N') ? 0 : 1);
  114. }
  115.